//
//  HorizontalCollectionView.m
//  BaseProject
//
//  Created by 王洋 on 2021/3/26.
//  Copyright © 2021 王洋. All rights reserved.
//
#import "ZZLayout.h"
#import "HorizontalCollectionView.h"

@interface HorizontalCollectionView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, assign) HorizontalCollectionType type;

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation HorizontalCollectionView

- (instancetype)initWithType:(HorizontalCollectionType)type {
    self = [super init];
    if (self) {
        self.type = type;
        [self layout];
    }
    return self;
}

- (void)layout {
    [self addSubview:self.collectionView];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.collectionView.frame = self.frame;
}

- (void)fillPromiseData:(NSMutableArray <CommonPromiseTagModel *>*)promiseArray {
    
    self.promiseArray = promiseArray.mutableCopy;
    [self.collectionView reloadData];
}

#pragma mark- 协议方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    switch (self.type) {
        case HorizontalCollectionType_storeDetail:{
            CommonPromiseTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CommonPromiseTagCellID" forIndexPath:indexPath];
            cell.itemTitleLab.text = self.promiseArray[indexPath.row].promiseLabelName;
            return cell;
        } break;
        case HorizontalCollectionType_hotelDetail: {
            CommonPromiseTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CommonPromiseTagCellID" forIndexPath:indexPath];
            cell.itemTitleLab.text = self.promiseArray[indexPath.row].promiseLabelName;
            return cell;
        } break;
        case HorizontalCollectionType_packageDetail:{
            CommonPromiseTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CommonPromiseTagCellID" forIndexPath:indexPath];
            cell.itemTitleLab.text = self.promiseArray[indexPath.row].promiseLabelName;
            return cell;
        } break;
        default:
            break;
    }
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCellID" forIndexPath:indexPath];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    switch (self.type) {
        case HorizontalCollectionType_storeDetail:{
            return self.promiseArray.count;
        } break;
        case HorizontalCollectionType_hotelDetail: {
            return self.promiseArray.count;
        } break;
        case HorizontalCollectionType_packageDetail:{
            return self.promiseArray.count;
        } break;
        default:
            break;
    }
    return 0;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

//返回这个UICollectionViewCell是否可以被选择
-(BOOL)collectionView:(UICollectionView*)collectionView shouldSelectItemAtIndexPath:(NSIndexPath*)indexPath{
    return NO;
}

#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*)indexPath{
    
    switch (self.type) {
        case HorizontalCollectionType_storeDetail:{
            CGFloat labWith =  [Utilities getStringWidthWithString:self.promiseArray[indexPath.row].promiseLabelName fontSize:10 andHeight:20] + 10;
            return CGSizeMake(labWith,20);
        } break;
        case HorizontalCollectionType_hotelDetail: {
            CGFloat labWith =  [Utilities getStringWidthWithString:self.promiseArray[indexPath.row].promiseLabelName fontSize:10 andHeight:20] + 10;
            return CGSizeMake(labWith,20);
        } break;
        case HorizontalCollectionType_packageDetail:{
            CGFloat labWith =  [Utilities getStringWidthWithString:self.promiseArray[indexPath.row].promiseLabelName fontSize:10 andHeight:20] + 10;
            return CGSizeMake(labWith,20);
        } break;
        default:
            break;
    }
    
    return CGSizeMake(CGFLOAT_MIN,CGFLOAT_MIN);
}

//定义每个UICollectionView 的边距
-(UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//边距的顺序是 上左下右
  return UIEdgeInsetsMake(0,0,0,0);
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout*layout=[[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, 0, 0) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.bounces = YES;
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"CommonPromiseTagCell" bundle:nil] forCellWithReuseIdentifier:@"CommonPromiseTagCellID"];


    }
    return _collectionView;
}

@end
